package com.gjun.struts.resource;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gjun.struts.bean.GjunRoleJsonBean;
import com.gjun.struts.bean.GjunsystemPrivilege;
import com.gjun.struts.bean.GjunsystemRole;

public class GjunRoleResourceDAO extends HibernateDaoSupport implements
		GjunRoleResourceIf {
    public boolean loadRoleByName(final String rolename) {
    	Long count = (Long)this.getHibernateTemplate().execute(new HibernateCallback(){
    		public Object doInHibernate(Session session) throws HibernateException, SQLException {
    			Query q = session.createQuery("select count(*) from GjunsystemRole role where role.rolename=?");
    			q.setParameter(0,rolename);
    			return q.uniqueResult();
    		}
    	});
    	
    	if(count>0) return true;
    	return false;
    }
	
	public List<GjunRoleJsonBean> loadAllRoles() throws Exception{
		// TODO Auto-generated method stub
		return (List<GjunRoleJsonBean>)this.getHibernateTemplate().execute(new HibernateCallback(){
			
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				// 投影查询
				Query q = session.createQuery("select new com.gjun.struts.bean.GjunRoleJsonBean(role.roleid,role.rolename,role.info) from GjunsystemRole role where role.id!=1");
				return q.list();
			}
		});
	}
	public void saveRole(final GjunsystemRole role, final String[] privileges) {  //[1,2,3]
		// TODO Auto-generated method stub
		List<GjunsystemPrivilege> usePris = (List<GjunsystemPrivilege>)
		this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				
				if(privileges!=null && privileges.length>0){
					String str = "";
					for(int i=0;i<privileges.length;i++){
						str+=privileges[i]+((i==privileges.length-1)?"":",");
					}
					
					Query q = session.createQuery("from GjunsystemPrivilege gp where gp.id in ("+str+")");
					return  q.list();
				}
				return null;
			}
		});
		
		if(usePris!=null){
			role.getPrivileges().addAll(usePris);
		}
		this.getHibernateTemplate().save(role);

	}
	
	public static void main(String[] args) {
		ApplicationContext context = new FileSystemXmlApplicationContext("src/applicationContext.xml");
		//BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
		GjunRoleResourceIf dao = (GjunRoleResourceIf)context.getBean("systemRoleDAO");
		
		GjunsystemRole role = new GjunsystemRole();
		role.setInfo("A");
		role.setRegdate(new Date());
		role.setRolename("B");
	
	    System.out.println(dao);
	    
	    dao.saveRole(role, null);
	}

	public void delRoleById(final int roleId) {
		System.out.println("--------------"+roleId);
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		session.beginTransaction();
		Query q1 = session.createSQLQuery("delete from gjunsystem_userrole where roleid=?");
		q1.setParameter(0, roleId);
		q1.executeUpdate();
		Query q2 = session.createSQLQuery("delete from gjunsystem_roleprivilege where rid=?");
		q2.setParameter(0, roleId);
		q2.executeUpdate();
		Query q3 = session.createSQLQuery("delete from gjunsystem_role where roleid=?");
		q3.setParameter(0, roleId);
		
		System.out.println(q3.executeUpdate());
		session.getTransaction().commit();
		session.close();
		// TODO Auto-generated method stub
//		this.getHibernateTemplate().execute(new HibernateCallback() {
//			
//			public Object doInHibernate(Session session) throws HibernateException,
//					SQLException {
//				
//				
//				// TODO Auto-generated method stub
//				Query q = session.createQuery("delete GjunsystemRole r where r.roleid=?");
//				q.setParameter(0, roleId);
//				return q.executeUpdate();
//			}
//		});
	}
	

}
